﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class views_regiter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["user"] != null)
            {
                DataTable dt = (DataTable)Session["user"];
                txtusername.Text = dt.Rows[0]["username"].ToString();
                txtfullname.Text = dt.Rows[0]["customername"].ToString();
                txtbrithday.Text = dt.Rows[0]["birthday"].ToString();
                txtemail.Text = dt.Rows[0]["email"].ToString();
                txtfone.Text = dt.Rows[0]["phone"].ToString();
                txtaddress.Text = dt.Rows[0]["address"].ToString();
                txtnote.Text = dt.Rows[0]["note"].ToString();
            }
        }
    }
    public DataTable getdata()
    {
        DataProvider data = new DataProvider();
        SqlConnection conn = data.Connect();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand("SELECT * as orderid FROM [db_flower].[dbo].[Customers] where username = ", conn);
            cmd.CommandType = CommandType.Text;
            dt.Load(cmd.ExecuteReader());

            return dt;
        }
        catch
        {
            return dt;
        }
        finally { conn.Close(); }
    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        DataProvider data = new DataProvider();
        SqlConnection conn = data.Connect();
        try
        {
            if (check_username() == true)
            {
                SqlCommand cmd = new SqlCommand("sp_customers_insert", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@customername", txtusername.Text);
                cmd.Parameters.AddWithValue("@birthday", DateTime.Parse(txtbrithday.Text));
                cmd.Parameters.AddWithValue("@address", txtaddress.Text);
                cmd.Parameters.AddWithValue("@phone", txtfone.Text);
                cmd.Parameters.AddWithValue("@username", txtusername.Text);
                cmd.Parameters.AddWithValue("@password", data.encryptData(txtpassword.Text));
                cmd.Parameters.AddWithValue("@email", txtemail.Text);
                cmd.Parameters.AddWithValue("@note", txtnote.Text);

                cmd.ExecuteNonQuery();

                Response.Write(@"<script language='javascript'>alert('đăng ký thành công!');</script>");
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Tài khoản đã tồn tại!\nXin vui lòng thử lại!');</script>");
                lbcheck.Text = "";
                SetFocus(txtusername);
                txtusername.Attributes.Add("onfocus", "this.select();");
            }
        }
        catch
        {
            Response.Write(@"<script language='javascript'>alert('đăng ký thất bại!\nXin vui lòng thử lại!');</script>");
        }
        finally { conn.Close(); }
    }
    protected void txtusername_TextChanged(object sender, EventArgs e)
    {
        check_username();
    }

    private bool check_username()
    {
        DataProvider data = new DataProvider();
        SqlConnection conn = data.Connect();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_select_username", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@username", txtusername.Text);
            dt.Load(cmd.ExecuteReader());

            if (dt.Rows.Count == 0)
            {
                lbcheck.Text = "<img src=\"../images/templates/check.jpg\" height=\"20\" alt=\"\" />";
                return true;
            }
            else
            {
                lbcheck.Text = "";
                return false;
            }
        }
        catch
        {
            return false;
        }
        finally { conn.Close(); }
    }
}